package com.cmcc.groupcontacts.numbersearch.activity;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cmcc.groupcontacts.base.BaseActivity;
import com.cmcc.groupcontacts.numbersearch.dao.NumberCategoryDao;
import com.cmcc.groupcontacts.numbersearch.model.NumberCategory;
import com.cmcc.groupcontacts.util.ValidationUtil;
import com.cmcc.groupcontacts.view.MyTitlePanel;
import com.yinhai.activity.R;

public class NumberSearchActivity extends BaseActivity implements
		OnClickListener {

	MyTitlePanel titlePanel;
	private Button btnCategory1;
	private Button btnCategory2;
	private Button btnCategory3;
	private Button btnCategory4;
	private Button btnCategory5;
	private Button btnCategory6;
	private Button btnCategory7;
	private Button btnCategory8;
	private Button btnCategory9;
	private Button btnCategory10;
	private Button btnCategoryMore;
	private EditText key;
	private ImageButton btnSearch;
	private ImageButton btnClear;
	private TextView tvLocation;
	private ImageView imgFresh;

	ArrayList<NumberCategory> listCategory;

	private boolean isInitalized = false; // 是否已初始化

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.number_search_activity);

		init();
		initEvent();

		// BaiduMapManager.init();
		// BaiduMapManager.start();
		// BaiduMapManager.callback = new BaiduMapManager.BaiduMapCallback() {
		// @Override
		// public void afterGetAddress(MKAddrInfo res) {
		// if (res == null) {
		// tvLocation.setText("定位失败,请确认已开启GPS或网络连接!");
		// } else {
		// tvLocation.setText(res.strAddr);
		// }
		// }
		// };

		titlePanel = (MyTitlePanel) findViewById(R.id.title_panel);
		titlePanel.setActivity(this);
	}

	private void init() {
		NumberCategoryDao dao = new NumberCategoryDao();
		listCategory = dao.load();

		btnCategory1 = (Button) findViewById(R.id.btn_category_1);
		btnCategory1.setOnClickListener(this);
		btnCategory1.setText(listCategory.get(0).name);
		btnCategory2 = (Button) findViewById(R.id.btn_category_2);
		btnCategory2.setOnClickListener(this);
		btnCategory2.setText(listCategory.get(1).name);
		btnCategory3 = (Button) findViewById(R.id.btn_category_3);
		btnCategory3.setOnClickListener(this);
		btnCategory3.setText(listCategory.get(2).name);
		btnCategory4 = (Button) findViewById(R.id.btn_category_4);
		btnCategory4.setOnClickListener(this);
		btnCategory4.setText(listCategory.get(3).name);
		btnCategory5 = (Button) findViewById(R.id.btn_category_5);
		btnCategory5.setOnClickListener(this);
		btnCategory5.setText(listCategory.get(4).name);
		btnCategory6 = (Button) findViewById(R.id.btn_category_6);
		btnCategory6.setOnClickListener(this);
		btnCategory6.setText(listCategory.get(5).name);
		btnCategory7 = (Button) findViewById(R.id.btn_category_7);
		btnCategory7.setOnClickListener(this);
		btnCategory7.setText(listCategory.get(6).name);
		btnCategory8 = (Button) findViewById(R.id.btn_category_8);
		btnCategory8.setOnClickListener(this);
		btnCategory8.setText(listCategory.get(7).name);
		btnCategory9 = (Button) findViewById(R.id.btn_category_9);
		btnCategory9.setOnClickListener(this);
		btnCategory9.setText(listCategory.get(8).name);
		btnCategory10 = (Button) findViewById(R.id.btn_category_10);
		btnCategory10.setOnClickListener(this);
		btnCategory10.setText(listCategory.get(9).name);
		// btnCategoryMore = (Button) findViewById(R.id.btn_category_more);
		// btnCategoryMore.setOnClickListener(this);

		key = (EditText) findViewById(R.id.text_number_search_search_key);
		btnSearch = (ImageButton) findViewById(R.id.btn_search);
		btnClear = (ImageButton) findViewById(R.id.btn_clear);
		tvLocation = (TextView) findViewById(R.id.number_search_location);
		imgFresh = (ImageView) findViewById(R.id.number_search_fresh);
		imgFresh.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				tvLocation.setText(getResources().getString(
						R.string.common_get_location));
				//BaiduMapManager.locate();
			}
		});
	}

	private void initEvent() {
		key.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				v.setFocusable(true);
				v.setFocusableInTouchMode(true);
				v.requestFocus();
			}
		});

		key.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				// 设置搜索框内容清除按钮显示状态
				String searchContent = key.getText().toString();
				if (searchContent == "") {
					btnClear.setVisibility(View.GONE);
				} else {
					btnClear.setVisibility(View.VISIBLE);
				}
			}
		});

		btnSearch.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (ValidationUtil.isNullOrEmpty(key.getText().toString())) {
					Toast.makeText(getBaseContext(), "还没输入关键字！",
							Toast.LENGTH_LONG).show();
					return;
				}

				searchByKey(key.getText().toString());
			}
		});
		btnClear.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				key.setText("");
				key.forceLayout();
			}
		});
	}

	@Override
	public void onClick(View v) {
		if (((String) v.getTag()).equals("more")) {
			ArrayList<String> list = new ArrayList<String>();
			for (int i = 7; i < listCategory.size(); i++) {
				list.add(listCategory.get(i).name);
			}
			final String[] services = list.toArray(new String[] {});
			AlertDialog.Builder builder = new AlertDialog.Builder(
					NumberSearchActivity.this);

			builder.setTitle(getString(R.string.number_search_more));
			builder.setItems(services,
					new android.content.DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							searchByCategory(8 + which);
						}
					});

			builder.show();
		} else {
			searchByCategory(Integer.valueOf((String) v.getTag()));
		}
	}

	private void searchByKey(String key) {
		Intent intent = new Intent(this, NumberSearchResultActivity.class);
		intent.putExtra("key", key);
		startActivity(intent);
	}

	private void searchByCategory(int categoryId) {
		Intent intent = new Intent(this, NumberSearchResultActivity.class);
		intent.putExtra("categoryId", categoryId);
		startActivity(intent);
	}

	// private Handler locationHandler = new Handler();
	// private Runnable locationErrTask = new Runnable() {
	// @Override
	// public void run() {
	// if (BaiduMapManager.myAddrInfo == null) {
	// tvLocation.setText("定位失败,请确认已开启GPS或网络连接!");
	// }
	// }
	// };

	@Override
	protected void onResume() {
		super.onResume();

		// if (isInitalized) {
		// BaiduMapManager.start();
		// }
		// BaiduMapManager.locate();
		// if (BaiduMapManager.myAddrInfo == null) {
		// locationHandler.postDelayed(locationErrTask, 20 * 1000);// 20s
		// }
	}

	@Override
	protected void onPause() {
		super.onPause();

		// if (isInitalized) {
		// BaiduMapManager.stop();
		// }
	}
}
